//
//  ZheDishViewController.m
//  Delicious
//
//  Created by gy on 16/5/5.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "ZheDishViewController.h"
#import "AppDelegate.h"
#import "drawerViewController.h"
#import "ZheDishListModel.h"
#import "ZheDishTableViewCell.h"
#import "ZheDishDetailViewController.h"
@interface ZheDishViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *allDishesArr;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ZheDishViewController
- (NSMutableArray *)allDishesArr
{
    if (!_allDishesArr)
    {
        _allDishesArr = [NSMutableArray array];
    }
    return _allDishesArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Click)];
    self.navigationItem.leftBarButtonItem = leftItem;
    

}
- (void)getData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 设置返回数据类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:ZheDishListUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // NSLog(@"%@",responseObject);
        NSArray *data = responseObject[@"data"];
        for (NSDictionary *dic in data)
        {
            ZheDishListModel *model = [[ZheDishListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allDishesArr addObject:model];
        }
        if (self.allDishesArr > 0)
        {
            [self createTableView];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
// 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZheDishTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([ZheDishListModel class])];
    [self.view addSubview:_tableView];
}
#pragma mark ---- tableView代理方法 -----------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allDishesArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZheDishListModel *model = self.allDishesArr[indexPath.row];
    BaseTableViewCell *cell = [TableViewFactory cellWithClass:[model class] tableView:tableView index:indexPath];
    [cell setDataWithModel:model];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZheDishListModel *model = self.allDishesArr[indexPath.row];
    ZheDishDetailViewController *zheDishDetailVc = [[ZheDishDetailViewController  alloc]init];
    zheDishDetailVc.ID = model.ID;
    zheDishDetailVc.imageUrl = model.imageUrl;
    [self.navigationController pushViewController:zheDishDetailVc animated:YES];
}

- (void)Click{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    drawerViewController *drawer = [delegate.VCDic objectForKey:@"drawer"];
    
    delegate.window.rootViewController = drawer;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
